__author__ = 'Knight'

import logging

def __get_error_file_handler(log_file):
    error_log_file = '{}_error'.format(log_file)
    console = logging.FileHandler(error_log_file, mode='a')
    console.setLevel(logging.ERROR)
    formatter = logging.Formatter('%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                                  '%Y-%m-%d %H:%M:%S')
    console.setFormatter(formatter)
    return console

def __get_file_handler(log_file):
    console = logging.FileHandler(log_file, mode='a')
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                                  '%Y-%m-%d %H:%M:%S')
    console.setFormatter(formatter)
    return console

def get_error_log(log_file, log_name=None):
    log_name = '' if log_name is None else log_name
    if len(logging.getLogger(log_name).handlers) == 2:
        return logging.getLogger(log_name)
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    info_console = __get_file_handler(log_file)
    error_console = __get_error_file_handler(log_file)
    logger = logging.getLogger(log_name)
    logger.addHandler(error_console)
    logger.addHandler(info_console)
    return logger

def get_info_log(log_file, log_name=None):
    log_name = '' if log_name is None else log_name
    if len(logging.getLogger(log_name).handlers) == 2:
        return logging.getLogger(log_name)
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    info_console = __get_file_handler(log_file)
    logger = logging.getLogger(log_name)
    logger.addHandler(info_console)
    return logger

def get_stream_log():
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    return logging.getLogger('')